package fourFragment.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xuegengwang.xuegengwang.R;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.HashMap;

import common.BaseActivity;
import common.Constant;
import https.InterNetController;
import utils.KeyBoard;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/5/6.
 * 提交意见
 */

@ContentView(R.layout.suggestion)
public class Suggestion extends BaseActivity implements View.OnClickListener {
    @ViewInject(R.id.common_back)
    private ImageView common_back;
    @ViewInject(R.id.suggestion_edt)
    private EditText suggestion_edt;
    @ViewInject(R.id.suggestion_confirm)
    private Button suggestion_confirm;
    @ViewInject(R.id.common_title)
    private TextView common_title;

    private String content;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    try {
                        JSONObject jsonObject = new JSONObject(String.valueOf(msg.obj));
                        String err_code = jsonObject.getString("err_code");
                        if ("0".equals(err_code)) {
                            Suggestion.this.finish();
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                        ToastUtils.shortToast("提交意见失败，请与客服联系");
                    }
                    break;
                default:
                    break;
            }
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        KeyBoard.showKeyBoard();
    }

    private void initView() {
        common_title.setVisibility(View.VISIBLE);
        common_title.setText("意见反馈");
        common_back.setOnClickListener(this);
        suggestion_confirm.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.common_back:
                this.finish();
                break;
            case R.id.suggestion_confirm:
                content = suggestion_edt.getText().toString();
                if(!TextUtils.isEmpty(content)) {
                    commitSuggestion();
                }else{
                    ToastUtils.shortToast("输入内容不能为空");
                }
                break;
            default:
                break;
        }
    }

    private void commitSuggestion() {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("content", content);
        new InterNetController(this, Constant.SUGGESTION, handler, hashMap, 0);
    }
}
